package com.itheima.serviceimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrdersettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrdersettingService.class)
@Transactional
public class OrdersettingServiceimpl implements OrdersettingService {


    @Autowired
    private OrdersettingDao ordersettingDao;

    //批量导入预约设置
    @Override
    public void add(List<OrderSetting> list) {

        //判断该日期是否以预约设置
        for (OrderSetting orderSetting : list) {
            Long ordersettingByDate = ordersettingDao.findOrdersettingByDate(orderSetting.getOrderDate());
            //if判断 如果以预约设置 则 修改该数值  ，如果没有则添加该预约设置
            if(ordersettingByDate>0){  //以预约设置 ，
                ordersettingDao.UpdateByDate(orderSetting);  //修改
            }else {
                ordersettingDao.add(orderSetting);  //添加
            }
        }

    }

    @Override
    public List<Map> FindAllByDate(String date) {  //date = 1997-03
        String start = date+"-01";
        String end = date+"-31";
        Map <String,String> map = new HashMap<String,String>();
        map.put("start",start);
        map.put("end",end);
        List<Map> maps = ordersettingDao.FindAllByDate(map);


        return maps;
    }

    //根据单个日子设置预约次数
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {

//        判断数据库中 该日子是否以设置过预约
        Long ordersettingByDate = ordersettingDao.findOrdersettingByDate(orderSetting.getOrderDate());

        if(ordersettingByDate>0){  //查询大于0则以往修改过,进行Update操作 , 反之 执行添加操作 Add
            //修改预约
            ordersettingDao.UpdateByDate(orderSetting);

        }else {
//            添加预约
            ordersettingDao.add(orderSetting);
        }

    }
}
